package com.jumi.microservice.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jumi.microservice.common.base.dto.BasePageResponse;
import com.jumi.microservice.common.base.dto.ResponseResult;
import com.jumi.microservice.common.utils.ResultGenerator;
import com.jumi.microservice.domain.Log;
import com.jumi.microservice.domain.vo.LogVO;
import com.jumi.microservice.service.LogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 日志表 前端控制器
 * </p>
 *
 * @author liuz
 * @since 2020-07-18
 */
@RestController
@Api(tags = "日志表 相关接口")
@RequestMapping("/auth/log")
public class LogController{

    @Autowired
    private LogService logService;


    @ApiOperation(value = "新增数据", notes = "新增一条日志表数据")
    @PostMapping("/add")
    public ResponseResult<Log> add(Log log) {
        logService.save(log);
        return ResultGenerator.genSuccessResult(log);
    }


    @ApiOperation(value = "删除数据", notes = "删除指定ID日志表数据")
    @ApiImplicitParam(name = "id", value = "ID")
    @PostMapping("/delete")
    public ResponseResult delete(@RequestParam Integer id) {
        logService.removeById(id);
        return ResultGenerator.genSuccessResult();
    }


    @ApiOperation(value = "更新数据", notes = "更新指定ID日志表数据")
    @PostMapping("/update")
    public ResponseResult<Log> update(Log log) {
        logService.updateById(log);
        return ResultGenerator.genSuccessResult(log);
    }


    @ApiOperation(value = "查询数据", notes = "获取指定ID日志表数据")
    @ApiImplicitParam(name = "id", value = "ID")
    @GetMapping("/detail")
    public ResponseResult<Log> detail(@RequestParam Integer id) {
        Log log = logService.getById(id);
        return ResultGenerator.genSuccessResult(log);
    }


    @ApiOperation(value = "查询分页", notes = "获取日志表分页列表数据")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "page", value = "当前页码"),
        @ApiImplicitParam(name = "size", value = "分页大小"),
    })
    @GetMapping("/list")
    public BasePageResponse list(
            @RequestParam(defaultValue = "0") Integer page,
            @RequestParam(defaultValue = "0") Integer size,
            LogVO vo) {
        LambdaQueryWrapper<Log> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(vo.getDepartmentId()!=null, Log::getDepartmentId, vo.getDepartmentId());
        wrapper.like(StringUtils.isNotEmpty(vo.getUserName()), Log::getUserName, vo.getUserName());
        wrapper.like(StringUtils.isNotEmpty(vo.getMobile()), Log::getMobile, vo.getMobile());
        wrapper.like(StringUtils.isNotEmpty(vo.getPriorityName()), Log::getPriorityName, vo.getPriorityName());
        wrapper.eq(vo.getPriorityId()!=null, Log::getPriorityId, vo.getPriorityId());
        wrapper.eq(vo.getOperateType()!=null, Log::getOperateType, vo.getOperateType());
        wrapper.like(StringUtils.isNotEmpty(vo.getOperateAction()), Log::getOperateAction, vo.getOperateAction());
        wrapper.ge(StringUtils.isNotEmpty(vo.getStartTime()), Log::getOperateTime, vo.getStartTime());
        wrapper.le(StringUtils.isNotEmpty(vo.getEndTime()), Log::getOperateTime, vo.getEndTime());
        wrapper.orderByDesc(Log::getOperateTime);
        IPage<Log> pageInfo = logService.page(
                new Page<>(page, size),wrapper);
        return ResultGenerator.genSuccessResultPage(pos2Vos(pageInfo.getRecords()),pageInfo.getTotal());
    }

    /**
     * 列表对象转换 po->vo
     * @param pos
     * @return
     */
    private List<LogVO> pos2Vos(List<Log> pos){
        List<LogVO> vos = pos.stream().map(p->{
            LogVO vo = null;
            try {
                vo = p.clone(LogVO.class);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return vo;
        }).collect(Collectors.toList());
        return vos;
    }

}
